var verify = require('../../../../../utils/verify-token.js')
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		cltag:false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var deviceId = options.deviceid;
		var productId = options.productid;
		this.setData({
			sh: app.globalData.screenHeight,
			sw: app.globalData.screenWidth,
			deviceId: deviceId,
			productId: productId
			
		})


	},
	//获取货道列表
	getAisleList() {
		var that = this;
		var baseUrl = app.globalData.baseUrl;
		var token = app.globalData.token;
		var deviceId = that.data.deviceId;
		wx.request({
			url: baseUrl + 'vending/device/' + deviceId + '/goods_aisle/list',
			header: {
				'AUTHORIZATION': token
			},
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					//console.log("aisleData", res.data.data)
					var aisleData = res.data.data;

					if (aisleData.length < 1) {
						wx.showModal({
							title: '暂无货道信息！！',
							content: '请先设置货道信息！！',
							success() {
								wx.navigateBack({

								})
							}
						})
					}
					aisleData.forEach(item => {
						for (var key in item) {
							if (key.indexOf('Price') >= 0) {
								item[key] = item[key].toFixed(2);
							}
						}
					})
					that.setData({
						aisleData: aisleData,
					})
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}

			}
		})
	},
	//设置副机
	setdeputy: function() {
		var deviceId = this.data.deviceId;
		var productId = this.data.productId;
		wx.navigateTo({
			url: 'set-deputy/set-deputy?deviceid=' + deviceId + '&productid=' + productId,
		})
	},
	//详情页
	detail: function(res) {
		var deviceId = this.data.deviceId;
		//console.log(res);

		var deviceGoodsAisleId = res.currentTarget.dataset.dsid;
		wx.navigateTo({
			url: 'aisle-detail/aisle-detail?deviceId=' + deviceId + '&deviceGoodsAisleId=' + deviceGoodsAisleId,
		})
	},
	sendtodev(){
		let that = this;
		wx.showModal({
			title:'提示',
			content:'您确定要进行此操作吗？',
			success(res){
				if (res.confirm) {
					that.sendToDevice();
					that.setData({
						cltag:true
					})
				}else{
					console.log("取消了")
				}
			}
		})
	},
	sendToDevice(){
		let that = this;
		let deviceId = that.data.deviceId;	
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/one_click_parameters',
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: 'post',			
			success(res) {
				that.setData({
					cltag:false
				});
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '成功',
						icon: 'success',
						duration: 2000
					})					
					that.getAisleList();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getAisleList();
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
